package com.yylending.las.authority.rsa;

import android.text.TextUtils;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class RsaPresenter extends Presenter<RsaModel, RsaPresenter.View> {
	private static final String TAG = RsaPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<RsaPresenter> {
		@Override
		public RsaPresenter create() {
			return new RsaPresenter();
		}
	}

	public interface View extends IView {
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new RsaModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getRsaKey() {
		if (TextUtils.isEmpty(Global.KEY_RSA)) {
			execute(null, RsaModel.MSG_GET_RSA, false);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void rsaResult(RsaModel.RSAResult result) {
		if (BaseResultEntity.isResponseSuccess(result) && null != result.getData() && !TextUtils.isEmpty(result.getData().key)) {
			Global.KEY_RSA = result.getData().key;
		}
	}
}